# This demonstrates a method for providing progress information during
# a package build.  It accurately takes into account the count of
# outstanding jobs to be done.  I.e. if only a few of the total
# targets need updating, it will print only that number.  The hope
# would be to use a mechanism simpler than cmake's, while still be
# accurate and useful.  It seems to be relatively fast, needing only
# two shell invocations plus a number of variable eval's.

SOURCES := 1.c 2.c 3.c 4.c 5.c 6.c 7.c 8.c 9.c 10.c 11.c 12.c 13.c

COUNT = 0
ifndef TOTAL
# If every recipe produced a single line during `make -n` then we could
# simply pipe through `wc -l`.  But counting occurences of a unique
# marker is more robust, and not much more costly.
PROGRESS_MARK := ++progress-mark++
TOTAL := $(shell $(MAKE) -n TOTAL=$(PROGRESS_MARK) $(MAKECMDGOALS) | grep --count -F -- $(PROGRESS_MARK))
endif
WIDTH := $(shell printf "%s" $(TOTAL) | wc -c)
PROGRESS = $(eval COUNT = $(shell expr $(COUNT) + 1)) printf "[%$(WIDTH)d/%d]" $(COUNT) $(TOTAL);

all: $(SOURCES)

$(SOURCES):
	@$(PROGRESS)echo "  GEN     $@"; touch $@; sleep `expr $$RANDOM % 2`

clean:
	rm -f $(SOURCES)
